<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form ref="formRef" label-width="150px" :model="form" :rules="rules">
      <el-form-item label="客满小记" prop="TrackMemo">
        <el-input v-model="form.TrackMemo" :rows="2" type="textarea" />
      </el-form-item>
      <el-form-item label="跟进方式" prop="TrackType">
        <el-select
          v-model="form.TrackType"
          size="large"
          placeholder="请选择跟进方式"
          style="width: 240px"
        >
          <el-option
            v-for="item in TrackTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="计划下次客满时间" prop="NextTrackTime">
        <el-date-picker
          v-model="form.NextTrackTime"
          type="datetime"
          placeholder="下次跟进时间"
          :size="size"
          :disabled-date="disabledDate"
          :default-time="new Date(2000, 2, 1, 8, 0, 0)"
          :disabled-hours="disabledHours"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/customerTrackingTracklist'

  export default defineComponent({
    name: 'VacancyEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        TrackTypeList: [
          {
            label: '电话',
            value: 1,
          },
          {
            label: '上门',
            value: 2,
          },
          {
            label: '客户到公司',
            value: 3,
          },
        ],
        form: {
          TrackClass: 0,
          TrackType: 1,
          TrackID: 0,
        },
        rules: {
          TrackMemo: [
            { required: true, trigger: 'blur', message: '请输入服务小记' },
          ],
          TrackType: [
            { required: true, trigger: 'blur', message: '请选择跟进方式' },
          ],

          NextTrackTime: [
            { required: true, trigger: 'blur', message: '请选择下次跟进时间' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })

      const disabledDate = (time) => {
        return time.getTime() < Date.now() - 8.64e7 // 8.64e7 毫秒数代表一天
      }

      const showEdit = (row) => {
        if (row == 0) {
          row = {
            TrackClass: 0,
            TrackType: 1,
            TrackID: 0,
          }
        }
        state.title = '客满小记'
        console.log(row, '12312312')
        state.form = Object.assign({}, row)
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const makeRange = (start, end) => {
        const result = []
        for (let i = start; i <= end; i++) {
          result.push(i)
        }
        return result
      }
      const disabledHours = () => {
        return makeRange(0, 7).concat(makeRange(21, 26))
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          console.log(state.form, 'state.form')
          if (valid) {
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        disabledDate,
        makeRange,
        showEdit,
        disabledHours,
        close,
        save,
      }
    },
  })
</script>
